<?php

/**
 * Description of BlockTimeForm
\* www.teuker.nl - mail@teuker.nl
 * 
 * @author francisc
 * i18n by Hartger
 */
class BlockTimeForm extends sfForm {

    public function configure() {
        $this->setWidgets(array(
            'time_start_hour' => new sfWidgetFormSelect(array('choices' => Valuelist::getTimeHourChoices())),
            'time_start_minute' => new sfWidgetFormSelect(array('choices' => Valuelist::getTimeMinuteChoices())),
            'time_end_hour' => new sfWidgetFormSelect(array('choices' => Valuelist::getTimeHourChoices())),
            'time_end_minute' => new sfWidgetFormSelect(array('choices' => Valuelist::getTimeMinuteChoices())),
            'date_start' => new sfWidgetFormInput(),
            'date_end' => new sfWidgetFormInput(),
            'reason' => new sfWidgetFormInput()
        ));

        $this->setValidators(array(
            'time_start_hour' => new sfValidatorString(array('min_length' => 2, 'max_length' => 2, 'required' => true)),
            'time_start_minute' => new sfValidatorString(array('min_length' => 2, 'max_length' => 2, 'required' => true)),
            'time_end_hour' => new sfValidatorString(array('min_length' => 2, 'max_length' => 2, 'required' => true)),
            'time_end_minute' => new sfValidatorString(array('min_length' => 2, 'max_length' => 2, 'required' => true)),
            'date_start' => new sfValidatorDate(array('required' => true)),
            'date_end' => new sfValidatorDate(array('required' => true)),
            'reason' => new sfValidatorString(array('max_length' => 100, 'min_length' => 3, 'required' => false))
        ));

        $this->validatorSchema->setPostValidator(
                    new sfValidatorCallback(array('callback' => array($this, 'checkTimes'))));

        $this->widgetSchema->setNameFormat('block_time[%s]');
    }
    
    public function setTaintedValue($field, $value) {
        $this->taintedValues[$field] = $value;
    }

    public function checkTimes($validator, $values) {
    	Util::loadHelper('I18N');
        
        /* check if date end is before date start */
        if (strtotime($values['date_end']) < strtotime($values['date_start'])) {
            throw new sfValidatorError($validator, __('Date end is before date start'));
        }

        if ($values['time_start_hour'] == $values['time_end_hour'])
            if ($values['time_end_minute'] <= $values['time_start_minute'])
                if (strtotime($values['date_end']) <= strtotime($values['date_start']))
                    throw new sfValidatorError($validator, __('Not a valid timespan'));

        return $values;
    }
}